// 工厂方法, 用于创建 一个 store module
import moduleCreator from '../moduleCreator'

import api from '@/api'

// moduleName 指向了
const modelName = 'account.move'

const Model = api.env(modelName)

const baseModel = {
  async action_post(payload) {
    const { id, fields } = payload
    const res = await Model.call('action_post', [id])
    console.log('action_post,', res)
    const res2 = await Model.browse_one(id, { fields })
    console.log('action_post,', res2)

    return res2
  },

  async create_payments(payload) {
    const { id, fields } = payload
    const wizard_model = 'account.payment.register'

    const wizard = await Model.wizard_create(id, {
      model: wizard_model,
      fields: {
        payment_date: null,
        journal_id: null,
        payment_method_id: null,
        group_payment: null
      }
    })
    console.log('wizard_create,', wizard)

    const res2 = await Model.wizard_submit(id, {
      model: wizard_model,
      values: { id: wizard.id },
      method: 'create_payments'
    })
    console.log('wizard_submit,', res2)
    const res = await Model.browse_one(id, { fields })
    console.log('browse_one,', res)
    return res
  }
}

const fields = {
  name: null,
  amount_total: null,
  state: null,
  invoice_payment_state: null,
  invoice_line_ids: null
}

const config = {
  model: modelName,
  fieldsForSearch: fields,
  fieldsForBrowse: fields
}

const state = {}

const mutations = {}

const actions = {
  async browse_one({ commit, state, dispatch }, payload) {
    const inv = await dispatch('_browse_one', payload)

    // if (so.invoice_ids__objects && so.invoice_ids__objects.length) {
    //   const invoce = so.invoice_ids__objects[0]
    //   commit('SET_invoice', invoce)
    // }

    // commit('SET_Options', ops)
    return new Promise(resolve => {
      resolve(inv)
    })
  },

  async action_post({ commit, state, dispatch }, payload) {
    const res = await baseModel.action_post({ ...payload, fields })
    // console.log('action_post', res)
    commit('SET_DataDict', res)
    return new Promise(resolve => {
      resolve(res)
    })
  },

  async create_payments({ commit, state, dispatch }, payload) {
    const res = await baseModel.create_payments({ ...payload, fields })
    console.log('create_payments', res)
    commit('SET_DataDict', res)
    return new Promise(resolve => {
      resolve(res)
    })
  }
}

export default moduleCreator(config, {
  state,
  mutations,
  actions
})

// // 这是最简单的写法
// export default moduleCreator(modelName)

// 如果有扩展, 则按照下面的写法
// const state = {
//   //
// }
// const mutations = {
//   //
// }
// const actions = {
//   //
// }

// export default moduleCreator(modelName, {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// })

// 如果 不继承, 用下面的写法
// const state = {
//   //
// }
// const mutations = {
//   //
// }
// const actions = {
//   //
// }

// export default {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// }
